<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class User extends Controller
{    

	//个人完整信息
	public function index()
	{
       $message=model('user')->findMessage(cookie('user')['id']);
       $hand=model('hand')->getHandByyid(cookie('user')['id']);
       return view('',[
          'message'=>$message,
          'hand'=>$hand
       ]);
	}

    //找回密码
    public function findPwd()
    {
       if(!request()->isPost()){
       	 return view();
       }else{
       	 $data=input('post.');
       	 unset($data['repwd']);
       	 $email=$data['email'];
       	 unset($data['email']);
       	 $data['code']=rand(1000,9999);
       	 $data['pwd']=md5($data['code'].$data['pwd']);
       	 $res=model('user')->save($data,['email'=>$email]);
       	 if($res){
            return $this->success('找回密码成功，去登陆','index/login');
       	 }else{
       	 	return $this->error('找回密码失败，请重试');
       	 }
       }
    }
    //验证邮箱是否存在
    public function checkEmail($email)
    {
    	$res=Db::name('user')->where('email',$email)->find();
    	if($res){
    		return 1;
    	}else{
    		return 0;
    	}
    }
    //获取验证码
    public function huoqu($email)
    {
    	$res=Db::name('user')->where('email',$email)->find();
    	if($res){
    		$message="你的验证码是{$res['code']}，请尽快输入";
    		$huoqu=\phpmailer\Email::send($res['email'],'验证码',$message);
    		if($huoqu){
    			return 1;
    		}else{
    			return 0;
    		}
    	}else{
    		return 0;
    	}
    }
    //验证码是否正确
    public function checkCode($email,$code)
    {
    	$res=Db::name('user')->where('email',$email)->find();
    	if($res['code']==$code){
    		return 1;
    	}else{
    		return 0;
    	}
    }

	//修改个人信息
	public function edit()
	{
	  $data=input('post.');
	  $validate=validate('user');
	  if($validate->scene('edit')->check($data)){
         $data['pwd']=md5('lishaole123');
         $res=model('user')->save($data,['id'=>cookie('user')['id']]);
         if($res){
            $search=Db::name('user')
               ->where('name',$data['name'])
               ->find();
            $search['exp']=Db::name('exp')->where('uid',$search['id'])->select();
            cookie('user',$search);
            return $this->success('修改成功');
         }else{
         	return $this->error('修改失败');
         }
	  }else{
	  	 return $this->error($validate->getError());
	  }
	}

	//发布信息（need/offer）
	public function release()
	{
		if(!request()->isPost()){
			$type=model('type')->order('orderT desc')->paginate(8);
			return view('',[
              'type'=>$type
			]);
		}
		$data=input('post.');
		$validate=validate('message');
		// halt($data);
		if($validate->check($data)){
			$data['uid']=cookie('user')['id'];
			$res=model('message')->save($data);
			if($res){
	           return $this->success('发布成功','index/index');
			}else{
	           return $this->error('发布失败，请核对信息');
			}
		}else{
			 return $this->error($validate->getError());
		}
	}

	//退出登陆
	public function loginout()
	{
		cookie('user',null);
		return $this->redirect("index/index");
	}
}